/**
 * Copyright 2018 orivil.com. All rights reserved.
 * Use of this source code is governed by a MIT-style
 * license that can be found at https://mit-license.org.
 */

package user_view

import (
	"gitee.com/tugoer/orivil/mux"
	"time"
	"net/http"
)

var Action = []*mux.Action{
	{
		Method: "GET", Route: "trigger", Name: "Trigger View",
		Params:mux.Parameters{
			{
				In:mux.Query,
				Schema:&triggerView{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &triggerView{}
			ctx.WarnParse(p)
			_, err := ctx.Cookie("uv")
			if err == http.ErrNoCookie {
				now := time.Now()
				hour, minute := now.Hour(), now.Minute()
				expire := now.Add(time.Duration(24-hour)*time.Hour-time.Duration(minute)*time.Minute)
				ctx.SetCookie(&http.Cookie{
					Path: "/",
					Name: "uv",
					Value: "true",
					Expires: expire,
				})
				Container.Click(p.Label, p.ID, false)
			} else {
				Container.Click(p.Label, p.ID, true)
			}
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tag},
			Summary: "触发点击事件",
			Description: "该事件由用户浏览器端触发, 则将自动抓取用户信息",
		},
	},

	{
		Method: "GET", Route: "user-views", Name: "Query User Views",
		Params:mux.Parameters{
			{
				In:mux.Query,
				Schema:&queryViews{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &queryViews{}
			ctx.WarnParse(p)
			var start, end *time.Time
			if p.Start != "" {
				s, err := time.Parse(DateTimeLayout, p.Start)
				if err != nil {
					ctx.ErrWarning(err)
					return
				}
				start = &s
			}
			if p.End != "" {
				e, err := time.Parse(DateTimeLayout, p.End)
				if err != nil {
					ctx.ErrWarning(err)
					return
				}
				end = &e
			}
			uvs, err := QueryUserViews(start, end, p.Label, p.Limit, p.Offset, p.Order)
			if err != nil {
				ctx.ErrWarning(err)
				return
			}
			ctx.SendJson(map[string]interface{}{
				"list": uvs,
			})
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tag},
			Summary: "查询浏览记录",
		},
	},

	{
		Method: "GET", Route: "count-user-views", Name: "Count User Views",
		Params:mux.Parameters{
			{
				In:mux.Query,
				Schema:&countViews{},
			},
		},
		Handler: func(ctx *mux.Context) {
			p := &countViews{}
			ctx.WarnParse(p)
			var start, end *time.Time
			if p.Start != "" {
				s, err := time.Parse(DateTimeLayout, p.Start)
				if err != nil {
					ctx.ErrWarning(err)
					return
				}
				start = &s
			}
			if p.End != "" {
				e, err := time.Parse(DateTimeLayout, p.End)
				if err != nil {
					ctx.ErrWarning(err)
					return
				}
				end = &e
			}
			total, err := CountLabel(start, end, p.Label)
			if err != nil {
				ctx.ErrWarning(err)
				return
			}
			ctx.SendJson(map[string]interface{}{
				"total": total,
			})
		},
		ApiDoc:&mux.ApiDoc{
			Tags:[]string{tag},
			Summary: "查询总记录数",
		},
	},
}

type countViews struct {
	Start string `desc:"开始时间，可选参数，必须是 2006-01-02 格式"`
	End string `desc:"结束时间，可选参数，必须是 2006-01-02 格式"`
	Label int `required:"Label 不能为空"`
}

type queryViews struct {
	countViews
	Limit int `required:"Limit 不能为空" num:"1<=x<=100" num-msg:"限制在1-100之间"`
	Offset int `required:"Offset 不能为空"`
	Order string `desc:"排序方式(可选参数), 'pv asc' 'pv desc' 'uv asc' 'uv desc'"`
}

type triggerView struct {
	Label int `required:"Label 不能为空"`
	ID int `required:"ID 不能为空"`
}
